package com.yunshipei.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.fsck.k9.mail.Message;
import com.yunshipei.model.ShareInfoModel;
import com.yunshipei.ui.dialog.DialogHelper;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.message.RichContentMessage;
import io.rong.message.TextMessage;

/**
 * Created by cike on 16/8/22.
 */
public class ShareUtil {
    private Context mContext;
    private DialogHelper.ShareDialog shareDialog = null;
    private ShareInfoModel shareInfoModel = null;
    private OnSendMessageSuccessListener onSendMessageSuccessListener = null;

    public ShareUtil(Context context) {
        this.mContext = context;
        shareDialog = new DialogHelper.ShareDialog(context);
    }

    public void setData(ShareInfoModel shareInfoModel) {
        this.shareInfoModel = shareInfoModel;
        shareDialog.setData(shareInfoModel);
    }

    public void showDialog() {
        shareDialog.showDialog(new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(shareDialog.getSendEdit().getWindowToken(), 0);
                sendMessage(shareInfoModel, shareDialog);

            }
        });
    }

    public void setOnDefineClickLisenter(OnSendMessageSuccessListener onSendMessageSuccessListener) {
        this.onSendMessageSuccessListener = onSendMessageSuccessListener;
    }

    private void sendMessage(ShareInfoModel shareInfoModel, final DialogHelper.ShareDialog shareDialog) {{
        String content= "分享来自  Enterplorer \n " + shareInfoModel.getUrl();

        RichContentMessage rcMessage = RichContentMessage.obtain(shareInfoModel.getHtmlTitle(),
                content, shareInfoModel.getImageUri(), shareInfoModel.getUrl());

        io.rong.imlib.model.Message msg  = new io.rong.imlib.model.Message();
        msg.setConversationType(shareInfoModel.getType());
        msg.setContent(rcMessage);
        msg.setTargetId(shareInfoModel.getShareModel().getTargetId());

        RongIM.getInstance().sendMessage(msg, shareInfoModel.getUrl(), null, new io.rong.imlib.IRongCallback.ISendMessageCallback() {
            @Override
            public void onAttached(io.rong.imlib.model.Message message) {
            }

            @Override
            public void onSuccess(io.rong.imlib.model.Message message) {
                Log.e("send Message", "send Success");
                shareDialog.cancel();
                if (onSendMessageSuccessListener != null)
                    onSendMessageSuccessListener.onClick();
            }

            @Override
            public void onError(io.rong.imlib.model.Message message, RongIMClient.ErrorCode errorCode) {
                Log.e("send Message", "errorCode = " + errorCode);
                shareDialog.cancel();
            }
        });

        //发送想要说的话
        if (!TextUtils.isEmpty(shareDialog.getEditValue())) {
            TextMessage textMessage = TextMessage.obtain(shareDialog.getEditValue());
            RongIM.getInstance().sendMessage(shareInfoModel.getType(), shareInfoModel.getShareModel().getTargetId(), textMessage, shareDialog.getEditValue(), null, null, null);
        }
    }


    }

    public static interface OnSendMessageSuccessListener {
        void onClick();
    }

}
